package com.oa.ylms.apps.actions.notice;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.google.gson.Gson;
import com.oa.ylms.apps.actions.BasicAction;
import com.oa.ylms.apps.business.archives.IStaffInfoService;
import com.oa.ylms.apps.business.notice.INoticeInfoService;
import com.oa.ylms.apps.business.organization.ISectionInfoService;
import com.oa.ylms.apps.entity.archives.StaffInfoValue;
import com.oa.ylms.apps.entity.notice.NoticeInfoValue;
import com.oa.ylms.apps.entity.notice.NoticeSectionValue;
import com.oa.ylms.apps.entity.notice.NoticeStaffValue;
import com.oa.ylms.apps.entity.organization.SectionInfoValue;

@SuppressWarnings("serial")
public class NoticeInfoAction extends BasicAction {

	private INoticeInfoService service;
	private NoticeInfoValue noticeInfo;
	private NoticeSectionValue noticeSection;
	private NoticeStaffValue noticeStaff;
	private List<NoticeInfoValue> list;
	private List<NoticeSectionValue> listSection;
	private List<NoticeStaffValue> listStaff;
	private List<SectionInfoValue> sectionList;
	private ISectionInfoService sectionInfoService;
	private IStaffInfoService staffInfoService;
	private Integer delete_id;
	private Integer query_id;
	private String noticeTitle;
	private String noticePlace;
	private String noticeIds;
	private String staffNames;
	private String sectionNames;
	private String staffIds;
	private String sectionIds;
	private String title;
	private String place;
	private String startTime;
	private String endTime;
	private String content;
	private String postScript;

	public String query() {
		try {
			list = service.queryNoticeInfoWithPage(noticePlace, noticeTitle,
					rows, currpage);
			totalpage = service.countTpages(noticePlace, noticeTitle, rows);
			count = service.getTotalNumber();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "show";
	}

	public String queryNoticeShow(){
		try {
			list = service.queryNoticeInfoWithPage(noticePlace, noticeTitle,
					rows, currpage);
			totalpage = service.countTpages(noticePlace, noticeTitle, rows);
			count = service.getTotalNumber();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "noticeShow";
	}
	
	public String queryDetail() throws Exception {
		try {
			noticeInfo = service.queryNoticeInfoByNoticeId(query_id);
			listSection = service.querySectionDetail(query_id);
			listStaff = service.queryStaffDetail(query_id);
		} catch (Exception e) {
			e.printStackTrace();
		}
		for (int i = 0; i < listStaff.size(); i++) {
			if (staffNames == null) {
				staffNames = "";
			}
			staffNames = staffNames
					+ listStaff.get(i).getStaffInfo().getArchiveInfo()
							.getStaffName() + ";";
		}
		for (int i = 0; i < listSection.size(); i++) {
			if (sectionNames == null) {
				sectionNames = "";
			}
			sectionNames = sectionNames
					+ listSection.get(i).getSectionInfo().getSectionName()
					+ ";";
		}
		Gson gson = new Gson();
		session.put("noticeInfo", noticeInfo);
		session.put("noticeSectionNames", sectionNames);
		session.put("noticeStaffNames", staffNames);
		String str = gson.toJson(session);
		try {
			response.setContentType("text/html; charset=UTF-8");
			response.setHeader("Cache-Control", "no-cache");
			response.getWriter().println(str);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

	public String addPage() {
		try {
			sectionList = sectionInfoService.queryAllSectionInfo();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "addPage";
	}

	public String deletePage() {
		staffInfo = (StaffInfoValue) session.get("login_staffInfo");
		int staffId = staffInfo.getStaffId();
		try {
			totalpage = service.countTpagesByStaffId(staffId, noticePlace,
					noticeTitle, rows);
			count = service.getTotalNumber();
			if (count % rows == 0 && totalpage + 1 == currpage) {
				currpage = currpage - 1;
				totalpage = currpage;
			}
			list = service.queryNoticeInfoByStaffIdWithPage(staffId,
					noticePlace, noticeTitle, rows, currpage);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "deletePage";
	}

	public String delete() {
		try {
			service.deleteNotice(delete_id);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "delete";
	}

	public String deleteMore() {
		try {
			service.deleteNotice(noticeIds);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "delete";
	}

	public String tip() {
		staffInfo = (StaffInfoValue) session.get("login_staffInfo");
		int staffId = staffInfo.getStaffId();
		int sectionId = staffInfo.getArchiveInfo().getPostInfo()
				.getSectionInfo().getSectionId();
		try {
			list = service.queryTipWithPage(sectionId, staffId,
					noticePlace, noticeTitle, rows, currpage);
			totalpage = service.countTpagesByTip(sectionId, staffId,
					noticePlace, noticeTitle, rows);
			count = service.getTotalNumber();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "tip";
	}

	public String add() throws Exception {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		Date start, end;
		staffInfo = (StaffInfoValue) session.get("login_staffInfo");
		int staffId = staffInfo.getStaffId();
		start = sdf.parse(startTime);
		end = sdf.parse(endTime);
		service.addNotice(staffId, title, place, start, end,
				content, postScript, staffIds, sectionIds);
		return "add";
	}

	public INoticeInfoService getService() {
		return service;
	}

	public void setService(INoticeInfoService service) {
		this.service = service;
	}

	public List<NoticeInfoValue> getList() {
		return list;
	}

	public void setList(List<NoticeInfoValue> list) {
		this.list = list;
	}

	public NoticeInfoValue getNoticeInfo() {
		return noticeInfo;
	}

	public void setNoticeInfo(NoticeInfoValue noticeInfo) {
		this.noticeInfo = noticeInfo;
	}

	public Integer getDelete_id() {
		return delete_id;
	}

	public void setDelete_id(Integer deleteId) {
		delete_id = deleteId;
	}

	public Integer getQuery_id() {
		return query_id;
	}

	public void setQuery_id(Integer queryId) {
		query_id = queryId;
	}

	public NoticeSectionValue getNoticeSection() {
		return noticeSection;
	}

	public void setNoticeSection(NoticeSectionValue noticeSection) {
		this.noticeSection = noticeSection;
	}

	public NoticeStaffValue getNoticeStaff() {
		return noticeStaff;
	}

	public void setNoticeStaff(NoticeStaffValue noticeStaff) {
		this.noticeStaff = noticeStaff;
	}

	public void setNoticeTitle(String noticeTitle) {
		this.noticeTitle = noticeTitle;
	}

	public String getNoticeTitle() {
		return noticeTitle;
	}

	public String getNoticePlace() {
		return noticePlace;
	}

	public void setNoticePlace(String noticePlace) {
		this.noticePlace = noticePlace;
	}

	public String getNoticeIds() {
		return noticeIds;
	}

	public void setNoticeIds(String noticeIds) {
		this.noticeIds = noticeIds;
	}

	public List<NoticeSectionValue> getListSection() {
		return listSection;
	}

	public void setListSection(List<NoticeSectionValue> listSection) {
		this.listSection = listSection;
	}

	public List<NoticeStaffValue> getListStaff() {
		return listStaff;
	}

	public void setListStaff(List<NoticeStaffValue> listStaff) {
		this.listStaff = listStaff;
	}

	public String getStaffNames() {
		return staffNames;
	}

	public void setStaffNames(String staffNames) {
		this.staffNames = staffNames;
	}

	public String getSectionNames() {
		return sectionNames;
	}

	public void setSectionNames(String sectionNames) {
		this.sectionNames = sectionNames;
	}

	public List<SectionInfoValue> getSectionList() {
		return sectionList;
	}

	public void setSectionList(List<SectionInfoValue> sectionList) {
		this.sectionList = sectionList;
	}

	public ISectionInfoService getSectionInfoService() {
		return sectionInfoService;
	}

	public void setSectionInfoService(ISectionInfoService sectionInfoService) {
		this.sectionInfoService = sectionInfoService;
	}

	public IStaffInfoService getStaffInfoService() {
		return staffInfoService;
	}

	public void setStaffInfoService(IStaffInfoService staffInfoService) {
		this.staffInfoService = staffInfoService;
	}

	public String getStaffIds() {
		return staffIds;
	}

	public void setStaffIds(String staffIds) {
		this.staffIds = staffIds;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getSectionIds() {
		return sectionIds;
	}

	public void setSectionIds(String sectionIds) {
		this.sectionIds = sectionIds;
	}

	public String getPlace() {
		return place;
	}

	public void setPlace(String place) {
		this.place = place;
	}

	public String getStartTime() {
		return startTime;
	}

	public void setStartTime(String startTime) {
		this.startTime = startTime;
	}

	public String getEndTime() {
		return endTime;
	}

	public void setEndTime(String endTime) {
		this.endTime = endTime;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public String getPostScript() {
		return postScript;
	}

	public void setPostScript(String postScript) {
		this.postScript = postScript;
	}

}
